//主场景所绑定的脚本文件
cc.Class({
    extends: cc.Component,

    properties: {
        //吃东西的动画
        eatAminima: {
            default: null,
            type: cc.Animation
        },
        // 分数
        scores: {
            default: null,
            type: cc.Label
        },
        //倒计时
        time_left: {
            default: null,
            type: cc.Label
        },

    },



    // use this for initialization
    onLoad: function () {

        //创建全局变量，方便其他页面调用
        if(!cc.globalData){
            cc.globalData = {}
            cc.globalData.score = 0;
        }
        //初始化数据
        this.inGame = false;//是否开始游戏了
        this.scores.string = 0;//当前页面显示的分数
    },

    //正式游戏时的触发
    startGame:function(){
        //每秒一次减去剩余时间
        var timeCallback = function () {
            this.time_left.string = +this.time_left.string-1
        }
        this.schedule(timeCallback, 1);
    },

    // 每一帧都会触发的方法
    update: function (dt) {
        //时间剩余为0的情况下切换场景到结果页面
        if(+this.time_left.string<=0&&this.inGame){
            this.inGame = false;
            cc.director.loadScene("resultScene");
            return;
        }
    },

    //用户点击屏幕事件
    clicked:function(){
        if(!this.inGame){
            this.inGame = true
            this.startGame()
        }
        this.eatAminima.play()
        this.scores.string = +this.scores.string+1;
        cc.globalData.score = this.scores.string;
    }
});
